package com.authorization.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chengyadong
 * @date 2023/12/13 14:20
 * @description 返回用户信息
 */
@RestController
public class EndPointController {
	/**
	 * 获取用户信息（userinfo路径系统存在内部处理方式，不会走到该方法）
	 * @return
	 */
	@GetMapping("/userinfo")
	public Authentication oauth2UserInfo() {
		// 获取的对象为在BearerTokenAuthenticationFilter过滤器封装的JwtAuthenticationToken对象
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null) {
			throw new RuntimeException("无有效认证用户！");
		}
		return authentication;
	}
}
